﻿using System.Web;
using System.Web.Mvc;

namespace CodeContrib.Web.Mvc.Html
{
	public static class GravatarExtensions
	{
		public static int DefaultImageSize { get; set; }

		static GravatarExtensions()
		{
			DefaultImageSize = 80;
		}

		public static MvcHtmlString GravatarImage(this HtmlHelper htmlHelper, string emailAddress)
		{
			return GravatarImage(htmlHelper, emailAddress, DefaultImageSize, GravatarDefaultImage.Default, null, null, false, GravatarImageRating.G, false);
		}

		public static MvcHtmlString GravatarImage(this HtmlHelper htmlHelper, string emailAddress, object htmlAttributes)
		{
			return GravatarImage(htmlHelper, emailAddress, DefaultImageSize, GravatarDefaultImage.Default, htmlAttributes, null, false, GravatarImageRating.G, false);
		}

		public static MvcHtmlString GravatarImage(this HtmlHelper htmlHelper, string emailAddress, int size)
		{
			return GravatarImage(htmlHelper, emailAddress, size, GravatarDefaultImage.Default, null, null, false, GravatarImageRating.G, false);
		}

		public static MvcHtmlString GravatarImage(this HtmlHelper htmlHelper, string emailAddress, int size, object htmlAttributes)
		{
			return GravatarImage(htmlHelper, emailAddress, size, GravatarDefaultImage.Default, htmlAttributes, null, false, GravatarImageRating.G, false);
		}

		public static MvcHtmlString GravatarImage(this HtmlHelper htmlHelper, string emailAddress, GravatarDefaultImage defaultImage)
		{
			return GravatarImage(htmlHelper, emailAddress, DefaultImageSize, defaultImage, null, null, false, GravatarImageRating.G, false);
		}

		public static MvcHtmlString GravatarImage(this HtmlHelper htmlHelper, string emailAddress, GravatarDefaultImage defaultImage, object htmlAttributes)
		{
			return GravatarImage(htmlHelper, emailAddress, DefaultImageSize, defaultImage, htmlAttributes, null, false, GravatarImageRating.G, false);
		}

		public static MvcHtmlString GravatarImage(this HtmlHelper htmlHelper, string emailAddress, int size, GravatarDefaultImage defaultImage)
		{
			return GravatarImage(htmlHelper, emailAddress, size, defaultImage, null, null, false, GravatarImageRating.G, false);
		}

		public static MvcHtmlString GravatarImage(this HtmlHelper htmlHelper, string emailAddress, int size, GravatarDefaultImage defaultImage, object htmlAttributes)
		{
			return GravatarImage(htmlHelper, emailAddress, size, defaultImage, htmlAttributes, null, false, GravatarImageRating.G, false);
		}

		public static MvcHtmlString GravatarImage(this HtmlHelper htmlHelper, string emailAddress, int size, GravatarDefaultImage defaultImage, object htmlAttributes, string defaultImageUrl, bool forceDefaultImage, GravatarImageRating rating, bool forceSecureRequest)
		{
			var image = new TagBuilder("img");
			image.Attributes.Add("alt", "Gravatar");
			image.Attributes.Add("class", "gravatar");
			image.Attributes.Add("height", size.ToString());
			image.Attributes.Add("width", size.ToString());

			if (emailAddress == null)
			{
				return MvcHtmlString.Empty;
			}

			var imageUrl = string.Format("{0}://{1}.gravatar.com/avatar/{2}?&d={3}{4}&r={5}&s={6}", (htmlHelper.ViewContext.HttpContext.Request.IsSecureConnection || forceSecureRequest) ? "https" : "http", (htmlHelper.ViewContext.HttpContext.Request.IsSecureConnection || forceSecureRequest) ? "secure" : "www", emailAddress.Trim().ToLower().ToMd5Hash(), (!string.IsNullOrEmpty(defaultImageUrl) ? HttpUtility.UrlEncode(defaultImageUrl) : defaultImage.ToDescription()), (forceDefaultImage) ? "&f=y" : string.Empty, rating.ToDescription(), size);

			image.Attributes.Add("src", imageUrl);

			var attributes = htmlAttributes.ToHtmlAttributes();

			foreach (var attribute in attributes)
			{
				var value = attribute.Value.ToString();

				if (!string.IsNullOrEmpty(value))
				{
					image.Attributes[attribute.Key] = value;
				}
			}

			return MvcHtmlString.Create(image.ToString());
		}
	}
}